package com.graduation_design.modules.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.AppSpecification;
import com.graduation_design.modules.app.controller.BaseController;
import com.graduation_design.service.AppSpecificationService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;

@RestController
@RequestMapping("/admin/spec")
public class AdminSpecController extends BaseController {

    @Resource
    AppSpecificationService appSpecificationService;

    @GetMapping("/list")
//    @RequiresPermissions("admin:specification:list")
    public Result list(String name) {
        return Result.success(appSpecificationService.page(getPage(),
                new QueryWrapper<AppSpecification>()
                        .like(StrUtil.isNotBlank(name), "name", name)
                        .orderByDesc("sort_order", "created")
        ));
    }

    @GetMapping("/info/{id}")
//    @RequiresPermissions("admin:specification:list")
    public Result info(@PathVariable Long id) {
        AppSpecification appSpecification = appSpecificationService.getById(id);
        if (StrUtil.isBlank(appSpecification.getOptions())) {
            appSpecification.setOptions("");
        }
        return Result.success(appSpecification);
    }

    @PostMapping("/save")
//    @RequiresPermissions(value = {"admin:specification:save", "admin:specification:update"}, logical = Logical.OR)
    public Result save(@Validated @RequestBody AppSpecification appSpecification) {

        if (appSpecification.getId() == null) {
            appSpecification.setCreated(LocalDateTime.now());
        } else {
            appSpecification.setUpdated(LocalDateTime.now());
        }

        appSpecificationService.saveOrUpdate(appSpecification);
        return Result.success(appSpecification);
    }

    @PostMapping("/delete")
//    @RequiresPermissions("admin:specification:delete")
    public Result delete(@RequestBody Long[] ids) {

        appSpecificationService.removeByIds(Arrays.asList(ids));
        return Result.success();
    }
}
